package com.irdstudio.apicenter.frame.tag;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import com.irdstudio.apicenter.frame.service.DataDicService;
import com.irdstudio.apicenter.frame.util.BeanUtils;

public class ParentTagSupport extends TagSupport {

	private static final long serialVersionUID = 1L;

	/**
	 * 在value不为null的情况下设置Tag的属性。
	 * 
	 * @param buffer
	 * @param name
	 * @param value
	 */
	protected void writeAttribute(StringBuffer buffer, String name, Object value) {
		if (value != null) {
			String valueStr = value.toString();
			if (valueStr != null) {
				valueStr = valueStr.replaceAll("'", "&#39;");
				buffer.append(" ").append(name).append("='").append(valueStr)
						.append("'");
			}
		}
	}

	/**
	 * 将指定内容输出到页面。
	 * 
	 * @param content
	 *            输出内容
	 */
	protected void outputContent(String content) {

		JspWriter out = pageContext.getOut();
		try {
			out.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 使用字典服务来进行字典转换
	 * 
	 * @param dicName
	 * @param dicOption
	 * @return
	 */
	protected String getDicOptionValue(String dicName, String dicOption) {
		if (StringUtils.isEmpty(dicName) || StringUtils.isEmpty(dicOption))
			return "";
		return BeanUtils.getBean(DataDicService.class).getValueText(dicName,
				dicOption);
	}
}
